package com.mklimek.frameviedoview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class FrameVideoView extends FrameLayout {

    private TextureViewImpl impl;
    private ImageView placeholderView;
    private Uri videoUri;
    private Context context;
    public FrameVideoView(Context context) {
        super(context);
        this.context = context;
        placeholderView = createPlaceholderView(context);
        impl = new TextureViewImpl(context);
        addView(impl);
        addView(placeholderView);
    }

    public FrameVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        placeholderView = createPlaceholderView(context);
        impl = new TextureViewImpl(context, attrs);
        addView(impl);
        addView(placeholderView);
    }

    public void setup(Uri videoUri, boolean supportZoom) {
        this.videoUri = videoUri;
        impl.init(placeholderView, videoUri, supportZoom);
    }
    
    private ImageView createPlaceholderView(Context context) {
        ImageView placeholder = new ImageView(context);
        placeholder.setScaleType(ImageView.ScaleType.FIT_CENTER);
        final LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        placeholder.setLayoutParams(params);
        return placeholder;
    }

    public void onResume(){
        impl.onResume();
    }

    public void onPause(){
        impl.onPause();
    }

    public void start() {
        impl.start();
    }

    public void pause() {
        impl.pause();
    }

    public void stop() {
        impl.stop();
    }

    public void setPlaceholderImageResource(int resId) {
        placeholderView.setImageResource(resId);
    }

    public void setPlaceholderImageDrawable(Drawable drawable) {
        placeholderView.setImageDrawable(drawable);
    }

    public void setPlaceholderImageBitmap(Bitmap bitmap) {
        placeholderView.setImageBitmap(bitmap);
    }

    public ImageView getPlaceholderImageView() {
        return placeholderView;
    }

    public void setFrameVideoViewListener(FrameVideoViewListener listener){
        impl.setFrameVideoViewListener(listener);
    }
}
